package com.danco.planner.viewcontrollers;

import com.danco.planner.controllers.core.ViewController;
import com.danco.planner.supportclasses.enumeration.DefaultEventType;
import com.danco.planner.supportclasses.enumeration.IEventType;
import com.danco.planner.supportclasses.enumeration.PlannerEventType;
import com.danco.planner.view.builders.AbstractViewsBuilder;
import com.danco.planner.view.builders.ConsoleViewsBuilder;
import com.danco.planner.view.core.AbstractButton;
import com.danco.planner.view.core.AbstractLabel;
import com.danco.planner.view.core.View;
import com.danco.planner.view.interfaces.IEventListener;

public abstract class AbstractPlannerVC extends ViewController implements IEventListener {

	protected AbstractButton showButton;
	protected AbstractButton hideButton;
	protected AbstractLabel contentLabel;
	
	public AbstractPlannerVC() {
		this(new View());
	}
	public AbstractPlannerVC(View view) {
		this(view,"null");
	}
	public AbstractPlannerVC(View view, String title) {
	
		super(view);
		
		if (this.baseView != null) {
					
			AbstractViewsBuilder builder = new ConsoleViewsBuilder();
			showButton = builder.buildButton("Show", "show", PlannerEventType.Show);
			hideButton = builder.buildButton("Hide", "hide", PlannerEventType.Hide);
			contentLabel = builder.buildLabel(getMainContent());
			AbstractLabel titleLabel = builder.buildLabel(title);
			AbstractButton backButton = builder.buildButton("Back", "back", DefaultEventType.Back);
			
			baseView.addSubview(titleLabel);
			baseView.addSubview(contentLabel);
			
			showButton.addListener(this);
			hideButton.addListener(this);
			backButton.addListener(this);
			
			baseView.addSubview(showButton);
			baseView.addSubview(hideButton);
			initAllOtherSubviews();
			baseView.addSubview(backButton);
			
			hideButton.setVisible(false);
			hideButton.setEnable(false);
			contentLabel.setVisible(false);
			contentLabel.setEnable(false);
			
		}
	}
	
	protected abstract void initAllOtherSubviews();
	protected abstract String getMainContent();
	
	@Override
	public void handleAction(Object arg0, IEventType arg1) {
		
		if (arg1 == DefaultEventType.Back) {
			popViewController();
		} 
		else if (arg1 == PlannerEventType.Hide) {
			hideContent();
		}
		else if (arg1 == PlannerEventType.Show) {
			showContent();
		}
	}
	
	protected void showContent() {
		
		showButton.setVisible(false);
		showButton.setEnable(false);
		hideButton.setVisible(true);
		hideButton.setEnable(true);
		contentLabel.setVisible(true);
		contentLabel.setEnable(true);
	}
	protected void hideContent() {
		
		showButton.setVisible(true);
		showButton.setEnable(true);
		hideButton.setVisible(false);
		hideButton.setEnable(false);
		contentLabel.setVisible(false);
		contentLabel.setEnable(false);
	}
}
